package com.bzkj.web.controller.ncc;

import com.bzkj.basicinfo.domain.ncc.NccBasicSupplier;
import com.bzkj.basicinfo.domain.ncc.dto.NccBasicSupplierDto;
import com.bzkj.basicinfo.domain.ncc.vo.SupplierExportVo;
import com.bzkj.basicinfo.service.ncc.NccBasicSupplierService;
import com.bzkj.common.annotation.LogC;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.enums.BusinessType;
import com.bzkj.common.utils.poi.CsvExportUtils;
import com.bzkj.common.utils.poi.ExcelPDFUtil;
import com.bzkj.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "供应商管理")
@RestController
@RequestMapping("/ncc/basic/Supplier")
public class NccBasicSupplierController extends BaseController
{

    @Autowired
    private NccBasicSupplierService supplierService;

    /**
     * NCC供应商信息定时任务
     */
    @GetMapping({"/nccBasicSupplierJob", "/nccBasicSupplierJob/{all}"})
    @PreAuthorize("@ss.hasPermi('ncc:Supplier:Job')")
    public void nccBasicSupplierJob(@PathVariable(required = false) String all)
    {
        supplierService.updateNccBasicSupplierJob(all);
    }


    /**
     * 修改供应商状态
     *
     * @Author wangzhichao
     * @Date 2024/12/05 15:43:15
     * @Param status 状态
     * @Param pkSupplier 主键
     */
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:stateEdit')")
    @PutMapping("/nccBasicSupplierEnableStatusUpdate")
    @ApiOperation(value = "修改状态接口", httpMethod = "PUT")
    @LogC(title = "ncc供应商信息", businessType = BusinessType.UPDATE,
            tableNames = "ncc_basic_supplier", pkFieldNames = "pk_supplier", content = "' 供应商ID：'+#pkSupplier")
    public AjaxResult nccBasicSupplierEnableStatusUpdate(@RequestParam(value = "status", defaultValue = "1") String status, @RequestParam(value = "pkSupplier") String pkSupplier)
    {
        return supplierService.nccBasicSupplierEnableStatusUpdate(status, pkSupplier);
    }

    /**
     * 修改供应商数据
     *
     * @Author wangzhichao
     * @Date 2024/12/05 16:04:06
     * @Param entity
     */
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:edit')")
    @PutMapping("/nccBasicSupplierUpdate")
    @ApiOperation(value = "修改接口", httpMethod = "PUT")
    @LogC(title = "ncc供应商信息", businessType = BusinessType.UPDATE,
            tableNames = "ncc_basic_supplier", pkFieldNames = "pk_supplier",content = "' 供应商名称：'+#entity.name")
    public AjaxResult nccBasicSupplierUpdate(@RequestBody NccBasicSupplier entity)
    {
        return supplierService.nccBasicSupplierUpdate(entity);
    }

    /**
     * 根据条件分页查询供应商信息
     *
     * @Author wangzhichao
     * @Date 2024/12/05 16:57:26
     * @Param dto 查询条件
     * @Param page 分页条件
     */
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:query')")
    @GetMapping("/nccBasicSupplierFindData")
    @ApiOperation(value = "查询接口", httpMethod = "GET")
    public TableDataInfo nccBasicSupplierFindData(NccBasicSupplierDto dto)
    {
        startPage();
        return getDataTable(supplierService.nccBasicSupplierFindData(dto));
    }

    /**
     * TODO 新增供应商信息
     *
     * @Author wangzhichao
     * @Date 2024/12/06 09:21:10
     * @Param entity
     */
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:add')")
    @PostMapping("/nccBasicSupplierSave")
    @ApiOperation(value = "新增接口", httpMethod = "POST")
    @LogC(title = "ncc供应商信息", businessType = BusinessType.INSERT,
            tableNames = "ncc_basic_supplier", pkFieldNames = "id", content = "' 供应商名称：'+#entity.name")
    public AjaxResult nccBasicSupplierSave(@RequestBody NccBasicSupplier entity)
    {
        return supplierService.nccBasicSupplierSave(entity);
    }

    /**
     * 模糊查询供应商名称
     *
     * @param name
     * @return
     */
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:query')")
    @GetMapping("findBaiscSuppleierName")
    public List<String> findBaiscSuppleierName(String name)
    {
        return supplierService.findBaiscSuppleierName(name);
    }

    /**
     * 定时同步供应商信息
     *
     * @return
     */
    @GetMapping("jobMethod")
    public AjaxResult jobMethodExecute()
    {
        return supplierService.jobMethodExecute(false);
    }


    /**
     * 导出供应商信息
     *
     * @param dto
     * @param response
     */
    @ApiOperation("导出excel")
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:export')")
    @PostMapping("supplierExport")
    @LogC(title = "ncc供应商信息", businessType = BusinessType.EXPORT)
    public AjaxResult supplierExport(NccBasicSupplierDto dto, HttpServletResponse response)
    {
        List<NccBasicSupplier> list = supplierService.nccBasicSupplierFindData(dto);
        List<SupplierExportVo> collect = list.parallelStream().map(item -> {
            SupplierExportVo vo = new SupplierExportVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        ExcelUtil<SupplierExportVo> util = new ExcelUtil<>(SupplierExportVo.class);
        util.exportExcel(response, collect, "供应商信息", "供应商信息", "supplier");
        return AjaxResult.success();
    }

    @ApiOperation("导出pdf")
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:export')")
    @PostMapping("supplierExportPdf")
    @LogC(title = "ncc供应商信息", businessType = BusinessType.EXPORTPDF)
    public AjaxResult supplierExportPdf(NccBasicSupplierDto dto, HttpServletResponse response) {
        List<NccBasicSupplier> list = supplierService.nccBasicSupplierFindData(dto);
        List<SupplierExportVo> collect = list.parallelStream().map(item -> {
            SupplierExportVo vo = new SupplierExportVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        ExcelPDFUtil<SupplierExportVo> util = new ExcelPDFUtil<>(SupplierExportVo.class);
        util.exportPDF(response, collect, "供应商信息", "供应商信息", getUsername(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), "全量");
        return AjaxResult.success();
    }

    @ApiOperation("导出csv")
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:export')")
    @PostMapping("supplierExportCsv")
    @LogC(title = "ncc供应商信息", businessType = BusinessType.EXPORTCSV)
    public AjaxResult supplierExportCsv(NccBasicSupplierDto dto, HttpServletResponse response) {
        List<NccBasicSupplier> list = supplierService.nccBasicSupplierFindData(dto);
        List<SupplierExportVo> collect = list.parallelStream().map(item -> {
            SupplierExportVo vo = new SupplierExportVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        try {
            CsvExportUtils.exportToWeb(response, "供应商信息.csv", collect);
            return AjaxResult.success("导出成功");
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("导出失败");
        }
    }

}
